package com.adcc.loadsheet.monitor.entity;

import com.adcc.loadsheet.monitor.util.AlarmEnum;
import com.adcc.loadsheet.monitor.util.Constant;
import com.adcc.loadsheet.monitor.util.StringUtil;

import java.util.HashSet;
import java.util.Set;

public class WebsiteMonitor extends BaseEntity implements Cloneable {

    // 地服网址url1
    private String url1;

    // 地服网址连接状态1
    private boolean connectStatus1;

    // 地服网址url2
    private String url2;

    // 地服网址连接状态2
    private boolean connectStatus2;

    // 地服网址状态
    private MonitorStatus status = MonitorStatus.ABNORMAL;

    // 异常状态码(0：正常，1：异常，2：告警)
    private String errorCode = "00";

    // 异常原因：XXX IS DOWN,XXX IS DOWN
    private String errorReason = "";

    // 本模块异常的节点
    private Set<String> alarmContent = new HashSet<>();

    // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
    private Set<String> unAlarmContent = new HashSet<>();

    public String getUrl1() {
        return url1;
    }

    public void setUrl1(String url1) {
        this.url1 = url1;
    }

    public boolean isConnectStatus1() {
        return connectStatus1;
    }

    public void setConnectStatus1(boolean connectStatus1) {
        this.connectStatus1 = connectStatus1;
    }

    public String getUrl2() {
        return url2;
    }

    public void setUrl2(String url2) {
        this.url2 = url2;
    }

    public boolean isConnectStatus2() {
        return connectStatus2;
    }

    public void setConnectStatus2(boolean connectStatus2) {
        this.connectStatus2 = connectStatus2;
    }

    public MonitorStatus getStatus() {
        return status;
    }

    public void setStatus(MonitorStatus status) {
        this.status = status;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorReason() {
        return errorReason;
    }

    public void setErrorReason(String errorReason) {
        this.errorReason = errorReason;
    }

    public Set<String> getAlarmContent() {
        return alarmContent;
    }

    public void setAlarmContent(Set<String> alarmContent) {
        this.alarmContent = alarmContent;
    }

    public Set<String> getUnAlarmContent() {
        return unAlarmContent;
    }

    public void setUnAlarmContent(Set<String> unAlarmContent) {
        this.unAlarmContent = unAlarmContent;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        WebsiteMonitor website = new WebsiteMonitor();
        website.setUrl1(url1);
        website.setConnectStatus1(connectStatus1);
        website.setUrl2(url2);
        website.setConnectStatus2(connectStatus2);
        website.setStatus(status);
        website.setErrorCode(errorCode);
        website.setErrorReason(errorReason);
        website.setAlarmContent(alarmContent);
        website.setUnAlarmContent(unAlarmContent);
        return website;
    }

    @Override
    public String toXML() throws Exception {
        return null;
    }

    @Override
    public String toJSON() throws Exception {
        return null;
    }

    /**
     * 异常状态码
     */
    public void buildErrorCode() {
        StringBuilder sb = new StringBuilder();
        char[] states = new char[2];
        states[0] = connectStatus1 ? '0' : '1';
        states[1] = connectStatus2 ? '0' : '1';
        for(char c : states){
            sb.append(c);
        }
        this.errorCode = sb.toString();

        // [2024-6-01：张丽娟：增加声音和弹窗告警的功能，系统支持对指定告警项进行声音和弹窗告警忽略的功能: 根据状态码获取正常节点和异常节点信息]
        // 本模块异常的节点
        Set<String> alarmContent = new HashSet<>();
        // 本模块正常的节点（用于判断告警确认后，部分节点正常后又异常需要重新告警）
        Set<String> unAlarmContent = new HashSet<>();
        // 保存到数据库的异常信息
        StringBuilder errorReasonBuilder = new StringBuilder();

        AlarmEnum enumWebOne = AlarmEnum.WEBSITE1;
        if (states[0] == '1') {
            errorReasonBuilder.append(String.format(Constant.LOG_DOWN, enumWebOne.getMysqlLogName()));
            alarmContent.add(enumWebOne.getItem());
        }else{
            unAlarmContent.add(enumWebOne.getItem());
        }

        AlarmEnum enumWebTwo = AlarmEnum.WEBSITE2;
        if (states[1] == '1') {
            errorReasonBuilder.append(String.format(Constant.LOG_DOWN, enumWebTwo.getMysqlLogName()));
            alarmContent.add(enumWebTwo.getItem());
        }else{
            unAlarmContent.add(enumWebTwo.getItem());
        }

        this.alarmContent = alarmContent;
        this.unAlarmContent = unAlarmContent;
        this.errorReason = StringUtil.stringBuilderToString(errorReasonBuilder);
    }
}
